<?php
namespace App\Module\Teacher\Service;

use App\Constants\Constant;
use App\Exception\AppException;
use App\Model\TeacherCredential;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Illuminate\Contracts\Hashing\Hasher;
use Qbhy\HyperfAuth\AuthManager;

class HomeService{

    /**
     * @Inject()
     * @var AuthManager $authManager
     */
    private $authManager;

    /**
     * @Inject()
     * @var Hasher $hash
     */
    protected $hash;

    public function info(){
        return $this->authManager->guard(Constant::TEACHER)->user();
    }

    public function password(RequestInterface $request){
        $password = $request->input("password");
        if(empty($password) || strlen($password) < 6){
            throw new AppException("密码必须大于5位");
        }

        /**
         * @var TeacherCredential $model
         */
        $model = $this->authManager->guard(Constant::TEACHER)->user()->account;
        $model->setAttribute("content",$this->hash->make($password));
        return $model->save();
    }
}